package cn.zswltech.gruul.web.api.util;

import cn.zswltech.gruul.common.exception.BaseException;
import cn.zswltech.gruul.common.exception.CommonErrorEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Jim
 * @version 1.0.0
 * @descripition:
 * @date 2024/2/29 22:03
 */
@Slf4j
public class GruulUtil {

    /**
     * 获取系统编号
     * @return
     */
    public static String getSystemCode(){
        String systemCode = null;
        try {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            systemCode = request.getHeader(CookieUtils.SYSTEM_CODE);
        } catch (Exception e) {
            log.error("获取系统号出错", e);
        }
        return systemCode;
    }

    /**
     * 校验系统编号
     */
    public static void checkSystemCode(){
        if (null == getSystemCode()){
            throw new BaseException(CommonErrorEnum.SYSTEM_CODE_MISS);
        }
    }
}
